#include "UnixSystemTray.h"
#include <QSystemTrayIcon>
#include <QTimer>

UnixSystemTray::UnixSystemTray(const QIcon &icon, QObject *parent):SystemTrayInterface(icon,parent)
{
    m_icon = icon;
    m_tray = new QSystemTrayIcon(icon,parent);
    m_blinkTimer = new QTimer;

    connect(m_tray,&QSystemTrayIcon::activated,this,&UnixSystemTray::onActivate);
    connect(m_blinkTimer,&QTimer::timeout,this,&UnixSystemTray::onBlink);
}

UnixSystemTray::~UnixSystemTray()
{
    if (m_tray) {
        delete m_tray;
        m_tray = nullptr;
    }
}

void UnixSystemTray::setIcon(const QIcon &icon)
{
    m_tray->setIcon(icon);
}

void UnixSystemTray::setContextMenu(QMenu *menu)
{
    m_tray->setContextMenu(menu);
}

void UnixSystemTray::setVisible(bool visiable)
{
    m_tray->setVisible(visiable);
}

void UnixSystemTray::startBlink()
{
    m_blinkTimer->start(500);
}

void UnixSystemTray::stopBink()
{
    m_blinkTimer->stop();
    setIcon(m_icon);
}

void UnixSystemTray::onActivate(QSystemTrayIcon::ActivationReason reason)
{
    emit this->activated(reason);
}

void UnixSystemTray::onBlink()
{
    static bool isBlink = false;
    QPixmap nullPix(64,64);
    nullPix.fill(Qt::transparent);
    isBlink = !isBlink;
    isBlink ? setIcon(m_icon) : setIcon(QIcon(nullPix));
}
